首先我們都需要先建置AudioManager
val audioManager:AudioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
getSystemService 得到手機控制音量的權限
建置完成後可以在Layout產生兩個Button並且給予ID
<Button
android:id="@+id/btn01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="btn01"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn02"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="btn02"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn01" />